package com.wust.http;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;


public class HttpUtil {
	public static HttpClient httpClient=new DefaultHttpClient();
	
	public static String getRequest(String url)throws Exception
	{
		HttpGet get=new HttpGet(url);
		System.out.print("start action");
		HttpResponse response=httpClient.execute(get);
		if(response.getStatusLine().getStatusCode()==200)
		{
			String result=EntityUtils.toString(response.getEntity());
			return result;
		}
		return null;
	}
	public static String postRequest(String url,Map<String,String>rawParams)throws Exception
	{
		HttpPost post=new HttpPost(url);
		List<NameValuePair>params=new ArrayList<NameValuePair>();
		for(String key:rawParams.keySet())
		{
			params.add(new BasicNameValuePair(key,rawParams.get(key)));
		}
		System.out.println("start action");
		post.setEntity(new UrlEncodedFormEntity(params,"utf-8"));
		HttpResponse response=httpClient.execute(post);
		if(response.getStatusLine().getStatusCode()==200)
		{
			String result=EntityUtils.toString(response.getEntity());
			System.out.println("result:"+result);
			return result;
		}
		return null;
	}
}
